<?php
/**
 * utility class for string processing
 * as a utility, it has not inheritance whatsoever,
 * so it stays static for the best performance
 *
 * must be manually included.
 *
 * @static
 *
 * @package com.commentor.util
 * @author  Bruce Li <toughgamer>
 */
class Util_String
{
    /**
     * filter slashes
     *
     * @param mixed $obj
     *
     * @return mixed $obj
     */
    public static function filterSlashes($obj)
    {
        if (is_array($obj)) {
            foreach ($obj as &$item) {
                $item = self::filterSlashes($item);
            }
        } else {
            $obj = stripslashes($obj);
        }

        return $obj;

    }// end filterSlashes


    /**
     * removeSpecialChars
     *
     * @param str $string
     *
     * @return str $string
     */
    public static function removeSpecialChars($string)
    {
         $marks = array(",", "&#39;");
         return str_replace($marks, '', $string);

    }// end removeSpecialChars


    /**
     * removeSpecialCharsAll
     *
     * @param str $string
     *
     * @return str $string
     */
    public static function removeSpecialCharsAll($string)
    {
        $old_pattern = array("/[^a-zA-Z0-9.]/", "/ +/", "/ $/");
        $new_pattern = array(" ", " ", "");
        $string = preg_replace($old_pattern, $new_pattern , $string);
        $string = stripslashes($string);

        return $string;

    }// end removeSpecialCharsAll


    /**
     * replaceSpaceWithDash
     *
     * @param str $string
     *
     * @return str $string
     */
    public static function replaceSpaceWithDash($string)
    {
        $string = str_replace(' ', '_', $string);
        return $string;

    }// end replaceSpaceWithDash


    /**
     * filterUrl
     *
     * @param str $string
     *
     * @return str $string
     */
    public static function filterUrl($string)
    {
        $old_pattern = array("/[^a-zA-Z0-9.]/", "/-+/", "/-$/");
        $new_pattern = array("-", "-", "");
        $string = preg_replace($old_pattern, $new_pattern , $string);
        $string = stripslashes($string);

        return $string;

    }// end filterUrl


    /**
     * public function: trim
     *
     * @param str $string
     * @param str $type
     *
     * @return str $string
     */
    public static function trim($string, $type = 'defaultTrim')
    {
        // switch...
        switch ($type) {
            case 'defaultTrim':
                return trim($string);
            case 'rightTrim':
                return rtrim($string);
            case 'lefttTrim':
                return ltrim($string);
            default:
                return trim($string);
        }

    }// end trim


    /**
     * filter type
     * basically, "s are not allowed
     * no HTML entities should be allowed
     *
     * @param str $title the title
     *
     * @access public
     *
     * @return str cleaned title
     */
    public static function filterTitle($title)
    {
        $title = str_replace('"', '\'', $title);
        $title = str_replace('&quote;', '\'', $title);
        $title = str_replace('&amp;', '&', $title);
        return $title;

    }// end filterTitle


    /**
     * get class name
     *
     * @param str $name name to be formatted
     *
     * @return str
     */
    public static function getClassName($name)
    {
    	if (!empty($name)) {
    		if (strpos($name, ' ')) {
    			$names = explode(' ', $name);
    			foreach ($names as &$n) {
    				$n = ucfirst(strtolower($n));
    			}
    			return join('_', $names);
    		}

    		return ucfirst(strtolower($name));
    	}

    }// end getClassName


    /**
     * camel case, _ to null, etc.
     *
     * @param str $name the name to change...
     * @param str $glue the glue, default to _
     *
     * @return str
     */
    public static function camelCase($name, $glue = '_')
    {
    	$name = strtolower($name);

    	if (strpos($name, $glue) !== false) {
    		$names = explode($glue, $name);
    		$name  = null;
    		foreach ($names as $n) {
                $name .= ucfirst($n);
            }
    	} else {
            $name = ucfirst($name);
    	}

    	return $name;

    }// end camelCase

}// end Util_String
?>